package com.wowangz.cms.dailyoperation.menuauthoritymanage.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.dailyoperation.menuauthoritymanage.pojo.MenuAuthorityManage;
import com.wowangz.cms.dailyoperation.menuauthoritymanage.service.MenuAuthorityManageService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;

@Scope("prototype")
@Controller
public class MenuAuthorityManageController extends BaseController{

	private static Logger logger = LoggerFactory.getLogger(MenuAuthorityManageController.class);
	
	/**
	 * 点击菜单"菜单权限管理"显示“菜单权限管理”列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/toMenuAuthorityManageList.web")
	public String toMenuAuthorityManageList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/dailyoperation/menuauthoritymanage/menuauthoritymanageList";
	}
	
	/**
	 * 打开“菜单权限管理”列表加载列表数据
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/querymenuauthoritymanageListInfoDataGrid.json")
	public void querymenuauthoritymanageListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService) IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			long resultCount = menuAuthorityManageService.findResultCount(map);
			List<MenuAuthorityManage> resultList = menuAuthorityManageService.findResultList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);
			
			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("menuid", DataTypeConstant.STRING);
			dataStyle.addDepict("menuname", DataTypeConstant.STRING);
			dataStyle.addDepict("url", DataTypeConstant.STRING);
			dataStyle.addDepictByFunction("type", SysConstant.RequestMenuType.class, "getName", new Class[]{long.class});
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转服务添加页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/toAddMenuAuthorityManage.web")
	public String toAddMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/dailyoperation/menuauthoritymanage/menuauthoritymanageAdd";
	}
	
	/**
	 * 验证
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/validattionMenuAuthorityManage.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void validattionMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService)IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		MenuAuthorityManage menuAuthorityManage = (MenuAuthorityManage)ServletUtils.getParametersToBaseEntity(request, MenuAuthorityManage.class);
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		StringBuffer errorMsg = new StringBuffer();
		try {
			if(menuAuthorityManageService.validattionMenuAuthorityManage(menuAuthorityManage)){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "dailyoperation.menuauthoritymanage.alert"));
			}
			
			if(StringUtils.isNotEmpty(errorMsg.toString())){
				userContext.setErrorMessage(errorMsg.toString());
			}
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/addMenuAuthorityManage.web", method = { RequestMethod.GET, RequestMethod.POST })
	public String addMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService)IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		MenuAuthorityManage menuAuthorityManage = (MenuAuthorityManage)ServletUtils.getParametersToBaseEntity(request, MenuAuthorityManage.class);
		try{
			menuAuthorityManage.setInputtime(new Date());
			menuAuthorityManage.setInputuserid(getUserContext().getLoginUser().getId());
			menuAuthorityManage.setModifytime(new Date());
			menuAuthorityManage.setModifyuserid(getUserContext().getLoginUser().getId());
			menuAuthorityManageService.insertMenuAuthorityManage(menuAuthorityManage);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return "/WEB-INF/jsp/dailyoperation/menuauthoritymanage/menuauthoritymanageAdd";
	}
	
	/**
	 * 跳转更新页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/toUpdateMenuAuthorityManage.web")
	public String toUpdateMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService)IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		String id = request.getParameter("id");
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		hashMap.put("id", id);
		MenuAuthorityManage menuAuthorityManage = menuAuthorityManageService.queryMenuAuthorityManageByID(hashMap);
		model.addAttribute("menuAuthorityManage",menuAuthorityManage);
		return "/WEB-INF/jsp/dailyoperation/menuauthoritymanage/menuauthoritymanageUpdate";
	}
	
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/doUpdateMenuAuthorityManage.web", method = { RequestMethod.GET, RequestMethod.POST })
	public String doUpdateMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService)IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		MenuAuthorityManage menuAuthorityManage = (MenuAuthorityManage)ServletUtils.getParametersToBaseEntity(request, MenuAuthorityManage.class);
		
		try{
			menuAuthorityManage.setModifytime(new Date());
			menuAuthorityManage.setModifyuserid(getUserContext().getLoginUser().getId());
			menuAuthorityManageService.updateMenuAuthorityManage(menuAuthorityManage);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/menuauthoritymanage/menuauthoritymanageUpdate";
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/dailyoperation/menuauthoritymanage/doDeleteMenuAuthorityManage.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void doDeleteMenuAuthorityManage(HttpServletRequest request, HttpServletResponse response, Model model) {
		MenuAuthorityManageService menuAuthorityManageService = (MenuAuthorityManageService)IssTransactionContainer.getInstance(MenuAuthorityManageService.class, false);
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		String id = request.getParameter("id");
		try{
			menuAuthorityManageService.deleteMenuAuthorityManage(Long.parseLong(id));
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert3"));
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
